package com.hb.trans.mvp.presenter

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent
import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.Base
import com.hb.trans.bean.PeriodPandect
import com.hb.trans.global.DataStore
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.TransInfosContract
import com.hb.trans.mvp.model.TransInfoModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class TransInfosPresenter @Inject constructor(model: TransInfoModel, var errorHandler: RxErrorHandler) :
    BasePresenter<TransInfosContract.View, TransInfosContract.Model>(model) {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun getPeriodListDetail() {
        val stu = DataStore.getInstance().getStudentInfo()
        stu?.let {
            mModel.getPeriodListDetail(it.stunum)
                .subscribeOn(Schedulers.io())
                .retryWhen(RetryWithDelay())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
                .subscribe(object : ErrorHandleSubscriber<Base<MutableList<PeriodPandect>>>(errorHandler) {
                    override fun next(r: Base<MutableList<PeriodPandect>>) {
                        if (r.code == 200) {
                            mRootView?.setupData(r.data)
                        } else {
                            mRootView?.showShortToast(r.message ?: "")
                        }
                    }
                })
        }

    }
}